//
//  CJBZRewardVideoAdapter.m
//  Pods
//
//  Created by shanghailingye on 2025/10/20.
//

#import "CJBZRewardVideoAdapter.h"

@interface CJBZRewardVideoAdapter()<CJRewardVideoAdDelegate>

@property (nonatomic, strong) CJRewardVideoAd *rewardVideoAd;

@property (nonatomic, assign) BOOL isBiddingRequest;

@property (nonatomic, strong) AMPSAdConfiguration *adConfiguration;

@end

@implementation CJBZRewardVideoAdapter

/// 竟胜回传
- (void)sendWinNotificationWithInfo:(NSDictionary *)winInfo {
    
}

/// 竞负回传
- (void)sendLossNotificationWithInfo:(NSDictionary *)lossInfo {
    
}

- (void)requestBiddingPriceWithSpaceId:(NSString *)spaceId
                       adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    self.isBiddingRequest = YES;
    [self loadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
}

- (void)loadAdWithSpaceId:(nonnull NSString *)spaceId adConfiguration:(nonnull AMPSAdConfiguration *)adConfiguration {
    if (CJBZSDKManagerAdapter.initState != kAdScopeMediationAdapterInitSDKStateSuccess) {
        __weak typeof(self)weakSelf = self;
        [CJBZSDKManagerAdapter startAsyncWithAppId:self.appId configuration:self.sdkConfiguration results:^(BOOL adapterResult) {
            __strong typeof(weakSelf)strongSelf = weakSelf;
            if (adapterResult) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [strongSelf lingYeLoadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
                });
            } else {
                if (strongSelf.isBiddingRequest) {
                    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:250401 userInfo:@{@"NSLocalizedDescription": @"AMPSGDTSDKManagerAdapter init fail"}];
                    if (strongSelf.bidDelegate && [strongSelf.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
                        [strongSelf.bidDelegate requestBiddingPriceFail:strongSelf error:error ext:adConfiguration.ext];
                    }
                } else {
                    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:270401 userInfo:@{@"NSLocalizedDescription": @"AMPSGDTSDKManagerAdapter init fail"}];
                    if (strongSelf.customDelegate && [strongSelf.customDelegate respondsToSelector:@selector(rewardedVideoAdLoadFail:error:ext:)]) {
                        [strongSelf.customDelegate rewardedVideoAdLoadFail:strongSelf error:error ext:adConfiguration.ext];
                    }
                }
            }
        }];
    } else {
        [self lingYeLoadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
    }
}

- (void)lingYeLoadAdWithSpaceId:(NSString *)spaceId adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    self.rewardVideoAd = [[CJRewardVideoAd alloc] initWithResourceId:spaceId userId:adConfiguration.userID extend:adConfiguration.extra];
    self.rewardVideoAd.delegate = self;
    [self.rewardVideoAd loadAdData];
    self.adConfiguration = adConfiguration;
    if (adConfiguration.videoSound && adConfiguration.videoSound == 1) {
        [self.rewardVideoAd setMute:false];
    }
}

- (void)loadC2SAdWithSpaceId:(NSString *)spaceId
                biddingToken:(id)biddingToken
             adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    if (self.isLoadAdSuccess) {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(rewardedVideoAdRenderSuccess:)]) {
            [self.customDelegate rewardedVideoAdRenderSuccess:self];
        }
    }
}

- (void)loadS2SAdWithSpaceId:(NSString *)spaceId
                biddingToken:(id)biddingToken
             adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    
}

- (NSInteger)eCPM {
    return [self.rewardVideoAd getECpm];
}

- (void)removeAd {
    if (_rewardVideoAd) {
        _rewardVideoAd.delegate = nil;
        _rewardVideoAd = nil;
    }
}

- (BOOL)isValid {
    return self.isLoadAdSuccess;
}

- (void)showRewardedVideoInViewController:(nonnull UIViewController *)viewController {
    [self.rewardVideoAd showFromRootViewController:viewController];
}

- (void)rewardVideoAdDidLoad:(CJRewardVideoAd *)rewardAd resourceId:(NSString *)resourceId {
    self.isLoadAdSuccess = YES;
    if (!self.isBiddingRequest) {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(rewardedVideoAdLoadSuccess:ext:)]) {
            [self.customDelegate rewardedVideoAdLoadSuccess:self ext:self.adConfiguration.ext];
        }
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(rewardedVideoAdRenderSuccess:)]) {
            [self.customDelegate rewardedVideoAdRenderSuccess:self];
        }
        return;
    }
    if ([self.rewardVideoAd getECpm] > 0) {
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceSuccess:ext:)]) {
            [self.bidDelegate requestBiddingPriceSuccess:self ext:self.adConfiguration.ext];
        }
    } else {
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
            NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:250401 userInfo:@{@"NSLocalizedDescription": @"AMPSGDTRewardedVideoAdapter ad bidding fail,ecpm less than 0"}];
            [self.bidDelegate requestBiddingPriceFail:self error:error ext:self.adConfiguration.ext];
        }
    }
}


- (void)rewardVideoAdOnShow:(CJRewardVideoAd *)rewardAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(rewardedVideoAdDidShow:)]) {
        [self.customDelegate rewardedVideoAdDidShow:self];
    }
}

- (void)rewardVideoAdLoadFailed:(CJRewardVideoAd *)rewardAd error:(NSError *)error {
    if (self.isBiddingRequest) {
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
            [self.bidDelegate requestBiddingPriceFail:self error:error ext:self.adConfiguration.ext];
        }
    } else {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(rewardedVideoAdLoadFail:error:ext:)]) {
            [self.customDelegate rewardedVideoAdLoadFail:self error:error ext:self.adConfiguration.ext];
        }
    }
    [self removeAd];
}

- (void)rewardVideoOnRewarded:(CJRewardVideoAd *)rewardAd requestId:(NSString *)requestId {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(rewardedVideoAdDidRewardEffective:rewardInfo:ext:)]) {
        [self.customDelegate rewardedVideoAdDidRewardEffective:self rewardInfo:[NSString stringWithFormat:@"%@",requestId] ext:self.adConfiguration.ext];
    }
}
- (void)rewardVideoAdOnClicked:(CJRewardVideoAd *)rewardAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(rewardedVideoAdDidClick:ext:)]) {
        [self.customDelegate rewardedVideoAdDidClick:self ext:self.adConfiguration.ext];
    }
}

- (void)rewardVideoAdOnClosed:(CJRewardVideoAd *)rewardAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(rewardedVideoAdDidClose:)]) {
        [self.customDelegate rewardedVideoAdDidClose:self];
    }
    [self removeAd];
}

- (void)dealloc {
    [self removeAd];
}

@end
